<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跑马灯轮播图特效</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .carousel {
            width: 650px;
            height: 360px;
            border: 1px solid #000;
            margin: 50px auto;
            position: relative;
           
        }

        .carousel ul {
            list-style: none;
        }

        .carousel ul li {
            position: absolute;
            top: 0;
            left: 0;
            /* 透明度都是0 */
            opacity: 0;
            transition: opacity 1s ease 0s;
        }
        .carousel ul li:first-child{
            opacity: 1;
        }

        .carousel .leftbtn {
            position: absolute;
            left: 20px;
            width: 50px;
            height: 50px;
            background-color: orange;
            border-radius: 50%;
            top: 50%;
            margin-top: -25px;
        }

        .carousel .rightbtn {
            position: absolute;
            right: 20px;
            width: 50px;
            height: 50px;
            background-color: orange;
            border-radius: 50%;
            top: 50%;
            margin-top: -25px;
        }
    </style>
</head>

<body>
    <div class="carousel">
        <ul id="list">
            <li>
                <img src="images/beijing/0.jpg" alt="">
            </li>
            <li>
                <img src="images/beijing/1.jpg" alt="">
            </li>
            <li>
                <img src="images/beijing/2.jpg" alt="">
            </li>
            <li>
                <img src="images/beijing/3.jpg" alt="">
            </li>
            <li>
                <img src="images/beijing/4.jpg" alt="">
            </li>
        </ul>
        <a href="javascript:;" class="leftbtn" id="leftbtn"></a>
        <a href="javascript:;" class="rightbtn" id="rightbtn"></a>
    </div>
    <script>
        //得到按钮和ul,ul整体进行运动
        var leftbtn = document.getElementById('leftbtn')
        var rightbtn = document.getElementById('rightbtn')
        var list = document.getElementById('list')
        var lis=list.getElementsByTagName('li')
        //当前是第几张图显示
        var idx=0;

        //节流
        var lock=true;

        rightbtn.onclick=function(){
            if(!lock)return;

            lock=false;
            lis[idx].style.opacity=0;
            idx++;
            if(idx>4){
                idx=0;
            }
            lis[idx].style.opacity=1; 

            setTimeout(function(){
                lock=true;
            },1000)  
        }

        leftbtn.onclick=function(){
            if(!lock)return;

            lock=false;

            lis[idx].style.opacity=0;
            idx--;
            if(idx<0){
                idx=4;
            }
            lis[idx].style.opacity=1; 

            setTimeout(function(){
                lock=true;
            },1000)  
        }
    </script>
</body>

</html>